/**********************************************************************
 Copyright (C) 2010 Teppo Eeva

 This file is part of Arithmetic Game.
 For more information, see <http://code.google.com/p/arithmetic-game/>

 Arithmetic Game is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 Arithmetic Game is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Arithmetic Game.  If not, see <http://www.gnu.org/licenses/>.
 **********************************************************************/

#include "highscoremanager.h"

#include <QtTest/QtTest>
#include <QDebug>
#include <QStandardItemModel>
#include "arithgame.h"



class TestHighScoreManager : public QObject
{
    Q_OBJECT

private slots:
    // Setup test case
    void init();

    // Actual test cases
    void doTestAllocate();                  // Test focus: Object allocation
    void doLoadHighScores();                // Test focus: Initialize default high scores
    void doGetNotDefinedHighScoreModel();   // Test focus: High scores model getter - Not defined
    void doGetEasyHighScoreModel();         // Test focus: High scores model getter - Easy
    void doGetModerateHighScoreModel();     // Test focus: High scores model getter - Moderate
    void doGetHardHighScoreModel();         // Test focus: High scores model getter - Hard
    void doResetHighScoreList();            // Test focus: Reset and re-initialize high scores

    // Cleanup test case
    void cleanup();

private:
    void verifyModelHeader(QStandardItemModel& model);

private:
    HighScoreManager* m_manager;
};


void TestHighScoreManager::init()
{
    m_manager = new HighScoreManager();
}

void TestHighScoreManager::cleanup()
{
    delete m_manager;
    QSettings settings("TestHighScoreManager");
    settings.clear();
}

void TestHighScoreManager::doTestAllocate()
{
    // Member exists
    QVERIFY( m_manager );
}

void TestHighScoreManager::doLoadHighScores()
{
    // Test case initialized default highscorelist since
    // the high scores does not exists
    m_manager->loadHighStores("TestHighScoreManager");
}



void TestHighScoreManager::doGetNotDefinedHighScoreModel()
{
    m_manager->loadHighStores("TestHighScoreManager");
    QStandardItemModel* model = m_manager->getHighScoreModel(NotDefined);
    verifyModelHeader( *model );
    QEXPECT_FAIL("", "For some reason following compare fails", Continue);
    QCOMPARE( model->rowCount(), 0 );
    delete model;
}

void TestHighScoreManager::doGetEasyHighScoreModel()
{
    m_manager->loadHighStores("TestHighScoreManager");
    QStandardItemModel* model = m_manager->getHighScoreModel(Easy);
    verifyModelHeader( *model );
    QCOMPARE( model->rowCount(), 3 );
    delete model;
}

void TestHighScoreManager::doGetModerateHighScoreModel()
{
    m_manager->loadHighStores("TestHighScoreManager");
    QStandardItemModel* model = m_manager->getHighScoreModel(Moderate);
    verifyModelHeader( *model );
    QCOMPARE( model->rowCount(), 3 );
    delete model;
}

void TestHighScoreManager::doGetHardHighScoreModel()
{
    m_manager->loadHighStores("TestHighScoreManager");
    QStandardItemModel* model = m_manager->getHighScoreModel(Hard);
    verifyModelHeader( *model );
    QCOMPARE( model->rowCount(), 3 );
    delete model;
}

void TestHighScoreManager::verifyModelHeader(QStandardItemModel& model)
{
    QCOMPARE( model.horizontalHeaderItem(0)->text(), QString("score"));
    QCOMPARE( model.horizontalHeaderItem(1)->text(), QString("name"));
    QCOMPARE( model.horizontalHeaderItem(2)->text(), QString("level"));
}

void TestHighScoreManager::doResetHighScoreList()
{
    m_manager->resetHighScores();
}

QTEST_MAIN(TestHighScoreManager)
#include "testhighscoremanager.moc"
